package leeon.forpeddy.htmlparse;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leeon.util.HtmlUtil;

public class ClassHtmlParser {
	
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	
	public ClassHtmlParser(String file) throws IOException {
		//读取
		BufferedReader br = new BufferedReader(new FileReader(file));
		StringBuffer sb = new StringBuffer();
		String s = null;
		while ((s = br.readLine()) != null) {
			sb.append(s);
		}
		br.close();
		String html = sb.toString();
		
		String table = HtmlUtil.findTag(html, "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#90C900\" width=\"100%\" id=\"AutoNumber1\" bgcolor=\"#FEF2D8\">", "</table>");
		String[] trs = HtmlUtil.findTable(table);
		for (int i = 1; i < trs.length ; i ++) {
			String[] tds = HtmlUtil.findTrLower(trs[i], "td");
			Map<String, String> row = new HashMap<String, String>();
			row.put("no", tds[2]);
			row.put("name", tds[3]);
			
			list.add(row);
		}
	}
	
	public List<Map<String, String>> getResults(String time, String area, String clz) {
		for (Map<String, String> row : list) {
			row.put("time", time);
			row.put("area", area);
			row.put("class", clz);
		}
		return list;
	}

	public static void main(String[] args) throws IOException {
		ClassHtmlParser chp = new ClassHtmlParser("D:\\workspace\\forpeddy\\misc\\2006春_常州_行政管理(行政).html");
		System.out.println(chp.getResults("2006春", "常州", "行政管理(行政)"));
	}

}
